package com.meritdata.cloud.data.service.datadirectory.service.impl;

import com.meritdata.cloud.data.service.datadirectory.entity.QTdDownloadPropose;
import com.meritdata.cloud.data.service.datadirectory.entity.TdShowApprovalInfo;
import com.meritdata.cloud.data.service.datadirectory.entity.TdDownloadPropose;
import com.meritdata.cloud.data.service.datadirectory.service.IDownloadProposeService;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DownloadProposeService implements IDownloadProposeService {

    @Autowired
    private JPAQueryFactory jpaQueryFactory;

    // 掉接口的对象
    public List<TdShowApprovalInfo> getDataApprovalList(String search, int state) {
        QTdDownloadPropose qTdDownloadPropose = QTdDownloadPropose.tdDownloadPropose;
        List<TdDownloadPropose> result;
        //查询全部数据审批信息
        if (state != 1 && state != 2 && state != 3) {
            result = jpaQueryFactory.selectFrom(qTdDownloadPropose).fetch();
        } else {
            result = jpaQueryFactory.selectFrom(qTdDownloadPropose).where(qTdDownloadPropose.approveStatus.eq(state)).fetch();
        }
        List<TdShowApprovalInfo> showApprovalInfoList = new ArrayList<>();
        // 根据id去掉接口查询数据表名称和所属域
        for (TdDownloadPropose td : result) {
            TdShowApprovalInfo tdShowApprovalInfo = new TdShowApprovalInfo();

            //表id通过接口获取表名称和所属域
            /*TdDataModel dataModels = ss.querydata(td.getTableId());
            if(!dataModels.getTableName().contains(search.trim())){
                continue;
            }
            tdShowApprovalInfo.setTableName(dataModels.getTableName());//表名称
            tdShowApprovalInfo.setDomainId(dataModels.getDomainId());//所属域*/

            //申请人id通过接口获取申请人
            //tdShowApprovalInfo.setApplyName(td.getApplyName());需要接口

            tdShowApprovalInfo.setId(td.getId());
            tdShowApprovalInfo.setTableId(td.getTableId());//表id
            tdShowApprovalInfo.setApplyField(td.getFilterText());
            tdShowApprovalInfo.setApplyId(td.getCreator());//申请人id
            tdShowApprovalInfo.setApplyTime(td.getCreateTime());//创建时间就是申请时间
            tdShowApprovalInfo.setApplyText(td.getFilterText());
            tdShowApprovalInfo.setPressNumber(td.getPressNumber());
            tdShowApprovalInfo.setApplyReason(td.getReasonText());
            tdShowApprovalInfo.setApproveStatus(td.getApproveStatus());
            tdShowApprovalInfo.setApproveText(td.getApproveText());
            tdShowApprovalInfo.setApproveTime(td.getUpdateTime());

            //tdShowApprovalInfo.setDeptName(td.getDeptName());部门名称
            showApprovalInfoList.add(tdShowApprovalInfo);

        }
        return showApprovalInfoList;
    }
}
